package packVistas;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableColumn;

import packLigaDeFutbol.GestorActuaciones;
import packLigaDeFutbol.GestorPartidos;
import packLigaDeFutbol.GestorTemporadas;
import packLigaDeFutbol.UtilControles;
import packLigaDeFutbol.comboValue;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.Calendar;

public class GestionAplazamientos extends JDialog {

	private JPanel contentPane;
	private JComboBox<comboValue> cmbPartido, cmbArbitro;
	private JComboBox<comboValue> cmbJornada_;
	private boolean cargando;
	private int idPartido;
	private int idArbitro;
	private int idJornada;

	public GestionAplazamientos(int pIdArbitro) {
		setModal(true);
		cargando = true;
		setTitle("Gesti\u00F3n de Aplazamientos");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 419, 312);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel norte = new JPanel();
		contentPane.add(norte, BorderLayout.NORTH);
		norte.setLayout(new FormLayout(new ColumnSpec[] {},
				new RowSpec[] {}));

		JPanel sur = new JPanel();
		contentPane.add(sur, BorderLayout.SOUTH);

		JPanel oeste = new JPanel();
		contentPane.add(oeste, BorderLayout.WEST);

		JPanel este = new JPanel();
		contentPane.add(este, BorderLayout.EAST);

		JPanel centro = new JPanel();
		contentPane.add(centro, BorderLayout.CENTER);
		centro.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("center:max(114dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("center:default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("top:max(21dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("min(50dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,}));

		JLabel lblNewLabel = new JLabel("Partido a aplazar:");
		centro.add(lblNewLabel, "2, 4, left, default");


		cmbPartido = new JComboBox<comboValue>();
		cmbPartido.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!cargando){
					JComboBox<comboValue> cb = (JComboBox<comboValue>)arg0.getSource();
					idPartido = ((comboValue)cb.getSelectedItem()).getValor();	
				}
			}
		});


		centro.add(cmbPartido, "4, 4, fill, default");

		JPanel panel = new JPanel();
		centro.add(panel, "2, 6, left, top");

		JLabel lblNewLabel_1 = new JLabel("Jornada de destino:");
		centro.add(lblNewLabel_1, "2, 8, left, default");

		cmbJornada_ = new JComboBox<comboValue>();
		cmbJornada_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Si esta cargando el combo no hacemos nada
				if (!cargando) {
					JComboBox<comboValue> cb = (JComboBox<comboValue>)arg0.getSource(); //obtenemos el combo
					idJornada = ((comboValue)cb.getSelectedItem()).getValor();			//obtenemos la fecha 

					//Cargamos el combo de los arbitros
					//como parametros le pasamos el combo de las fechas y el resultset de todos los arbitros disponibles
					UtilControles.cargarCombo(cmbArbitro, GestorPartidos.get().obtenerArbitrosDisponiblesEsaJornadaConProvinciaDistintaALaDeLosEquiposDelPartido(idPartido));
					//Cargamos la tabla con la lista de goles del partido
				}
			}

		});
		cmbJornada_.setPreferredSize(new Dimension(100, 20));


		centro.add(cmbJornada_, "4, 8, fill, default");

		JLabel lblNewLabel_3 = new JLabel("\u00C1rbitros disponibles:");
		centro.add(lblNewLabel_3, "2, 14, left, center");

		cmbArbitro = new JComboBox<comboValue>();
		cmbArbitro.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JComboBox<comboValue> cb = (JComboBox<comboValue>)arg0.getSource();
				idArbitro = ((comboValue)cb.getSelectedItem()).getValor();	
			}
		});


		centro.add(cmbArbitro, "4, 14, fill, bottom");

		JButton btnNewButton_2 = new JButton("Anotar incidencia");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				//insertamos la actuacion del aplazamiento en la base de datos
				if (GestorActuaciones.get().crearAplazamiento(idPartido,idJornada,idArbitro)){

					JOptionPane.showMessageDialog(new JFrame(), 
							"Se han modificado correctamente la fecha y arbitro del partido.", 
							getTitle(),JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(new JFrame(), 
							"No se ha podido actualizar el partido.", 
							getTitle(),JOptionPane.WARNING_MESSAGE);
				}
				cmbArbitro.setSelectedIndex(0);	
			}
		});
		centro.add(btnNewButton_2, "2, 20, center, default");

		JButton btnNewButton_1 = new JButton("Cerrar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		centro.add(btnNewButton_1, "4, 20, fill, default");
		UtilControles.cargarCombo(cmbPartido, GestorPartidos.get().obtenerPartidosDeArbitro(pIdArbitro));
		UtilControles.cargarCombo(cmbJornada_, GestorPartidos.get().ObtenerFechasDeJornadas(GestorTemporadas.get().temporadaActual()));

		cargando = false;
	}

}
